// pages/MainPage/MainPage.js
const db = wx.cloud.database()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({

    /**
     * 页面的初始数据
     */
    data: {
      active: "mainpage",
      information:[]
    },
    all(){
      wx.navigateTo({
        url: '../consultation/consultation',
      })
    },
    onChange(e) {
      // event.detail 的值为当前选中项的索引
      console.log(e);
      this.setData({ active: e.detail });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                active: 0
            })
        }
        this.getPost()
        this.getinformation()
    },
    
    getPost() {
        let that = this
        db.collection('articleDB').where({
          area: db.command.or(["日常交流","桌游攻略","情报速递","桌游战报"])
        }).orderBy("createTime","desc").get({
            success: (res) => {
                let post = res.data
                this.setData({post})
                for (let i = 0; i < post.length; i++) {

                    // 点赞、收藏、评论
                    let like = post[i].like
                    that.setData({
                        ['post[' + i + '].likeNumber']: Object.keys(like).length,
                        ['post[' + i + '].commentNumber']: post[i].comment.length
                    })
                    
                    // 作者信息
                    db.collection('userDB').where({
                        _openid: post[i]["_openid"]
                    }).get({
                        success: (res) => {
                          console.log(res)
                            if ( res.data.length == 0 ) {
                                post[i]['nickname'] = "微信用户"
                                post[i]["avatar"] = defaultAvatarUrl
                            }
                            else {
                                post[i]["nickname"] = res.data[0].nickname
                                if ( res.data[0].avatar )
                                    post[i]["avatar"] = res.data[0].avatar
                                else
                                    post[i]["avatar"] = defaultAvatarUrl
                            }
                            that.setData({post})
                        }
                    })
                }
            }
        })
    },
    getinformation(){
      let that = this
      db.collection('articleDB').where({
        area:"捉友资讯"
      }).get({
        success:(res) => {
          
          let information = res.data
          console.log(information);
          this.setData({information})
        }
      })
    },
    gotoInformation(e) {
      console.log(e);
        let id = e.currentTarget.dataset.id
        console.log(id);
        // let id_drawu = id.filter((i) =>{
        //   return i.
        // })
        wx.navigateTo({
            url: '../Article/Article',
            success: function(res) {
                res.eventChannel.emit('acceptDataFromOpenerPage',{id:id});
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    gotoCreateActivity() {
        wx.navigateTo({
          url: '../CreateActivity/CreateActivity',
        })
    },
    gotoAI() {
        wx.navigateTo({
            url: '../ai/ai',
        })
    },
    gotoArticle(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../Article/Article',
            success: function(res) {
                res.eventChannel.emit('acceptDataFromOpenerPage',{id:id});
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.getPost()
        setTimeout(function(){
            wx.stopPullDownRefresh();
        },300);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})